package com.yunduo.schedule;

import com.yunduo.common.RetunData.statuscode.impl.ServerCode;
import com.yunduo.entity.Log;
import com.yunduo.service.LogService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class AccountSchedule {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private LogService logService;

    //重置今日登录总人数 每天6点刷新 保存数据到数据库
    @Scheduled(cron = "0 0 6 * * ?")
    public void resetToDayLoginTotal(){
        SetOperations setOperations = redisTemplate.opsForSet();
        String total = String.valueOf(setOperations.size(ServerCode.TODAY_LOGIN_ACCOUNT_TOAL.getInfo()));
        Log log = new Log()
                    .setEvent(ServerCode.LOGIN_TOTAL_LOG.getInfo())
                    .setResult(total);
        logService.save(log);
        redisTemplate.delete(ServerCode.ONLINE_ACCOUNT_TOTAL.getInfo());
        redisTemplate.delete(ServerCode.TODAY_LOGIN_ACCOUNT_TOAL.getInfo());
    }
}
